#ifdef WIN32
#include "..\afx.h"
#else
#include "../afx.h"
#endif
#include "query.h"
#include<iostream>
using std::cerr;
#ifdef DEBUG
	using std::cout;
	using std::endl;
#endif

const string Item::ID = "id";
const string Item::Password = "password";
const string Item::Mail="mail";
const string Item::Dot = ",";
const string Item::Find = "find";
const string Item::LP= "(";
const string Item::RP=")";

bool item(GrammarExpression& express){
	if(express.Match(Item::ID)){
	#ifdef DEBUG
		cout << "match id" << endl;	
	#endif
	}
	else if(express.Match(Item::Password)){
	#ifdef DEBUG
		cout << "match password" << endl;		
	#endif

	}
	else if(express.Match(Item::Mail)){
	#ifdef DEBUG
		cout << "match mail" << endl;	
	#endif

	}
	else{
		//cerr << "syntax error at index " << express.Token << endl;
		express.Error("syntax error");
		return false;

	}
	return true;
}

bool R(GrammarExpression& express){
	if(express.Match(Item::Dot)){
		if(item(express)){
			return R(express);
		}
		else{
			return false;
		}
	}
	return true;
}

bool List(GrammarExpression& express){
	if(item(express)){
		if(R(express)){
			return true;
		}
		else{
			return false;
		}
	}
	else{
		return false;
	}
}

bool program(GrammarExpression& express){
	if(express.Match(Item::Find)){
		if(express.Match(Item::LP)){
			 if(List(express)){
			 	if(express.Match(Item::RP)){
					return true;
				}
				else{
					//cerr << "miss right parentheses at index " << express.Token << endl;
					express.Error("miss right parentheses");
					return false;
				}
			 }
			 else{
			 	return false;
			 }
		}
		else{
			//cerr << "miss left parentheses at index " << express.Token << endl;	
			express.Error("miss left parentheses");
			return false;
		}
	}
	else{
			//cerr << "miss find at index "<< express.Token << endl;
			express.Error("miss \"find\"");
			return false;
	}
}


